package com.prs.crm.action.fs;

import java.io.File;
import java.util.Collection;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.prs.crm.annotation.Clew;
import com.prs.crm.context.ToolKits;
import com.prs.crm.domain.fs.BankAccount;
import com.prs.crm.domain.fs.TransferAccount;
import com.prs.crm.service.fs.BankAccountService;
import com.prs.crm.service.fs.TransferAccountService;

public class TransferAccountAction extends BaseAction{
	private static final long serialVersionUID = 1L;
	private TransferAccount transferAccount;
	private TransferAccountService transferAccountService;
	private PaginationSupport pagination;
	private Integer[] ids;
	
	private BankAccountService bankAccountService;
	
	private Collection<BankAccount> bankAccounts;
	private File upload;
	private String uploadFileName;
	private String bank;
	
	@Action("listTransfer")
	public String listTransfer() {
		return SUCCESS;
	}
	@Action("listTransferByBank")
	public String listTransferByBank(){
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(TransferAccount.class);
		criteria.addOrder(Order.desc("recordTime"));
		pagination = this.getPaginationHelper().prepare(pagination);
		if(bank != null){
			if(!"all".equals(bank)){
				bank = bank.split(",")[0];
				if(ToolKits.isInteger(bank)){
					criteria.createAlias("srcAccount", "ba");
					criteria.add(Restrictions.eq("ba.id", Integer.parseInt(bank)));
				}
			}
		}
		this.setPagination(this.getTransferAccountService().listByPage(criteria, pagination));
		return SUCCESS;
	}
	
	
	@Action("alterTransfer")
	public String alterTransfer() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(TransferAccount.class);
		criteria.addOrder(Order.desc("recordTime"));
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getTransferAccountService().listByPage(criteria, pagination));
		return SUCCESS;
	}
	
	@Action("editTransfer")
	public String editTransfer() {
		bankAccounts = this.getBankAccountService().getAllBankAccount();
		return SUCCESS;
	}
	
	@Action("viewTransfer")
	public String viewTransfer() {
		return SUCCESS;
	}
	
	@Clew(results={"success"})
	@Action(value="submitTransfer",results={@Result(name="success",type="redirect",location="editTransfer"),
			@Result(name="error",type="chain",location="editTransfer")})
	public String submitTransfer() {
		if (transferAccount.getTitle() == null
				|| "".equals(transferAccount.getTitle().trim())) {
			addActionError("主题不能为空");
			return ERROR;
		}
		if (transferAccount.getAimAccount() == null) {
			addActionError("入账账户不能为空");
			return ERROR;
		}
		if (transferAccount.getTransferTime() == null) {
			addActionError("入帐时间不能为空");
			return ERROR;
		}
		if (transferAccount.getMoney() == null) {
			addActionError("入账金额不能为空");
			return ERROR;
		}
		if (transferAccount.getTransferPerson() == null) {
			addActionError("入账人不能为空");
			return ERROR;
		}
		this.getTransferAccountService().submitTransfer(transferAccount, upload, uploadFileName);
		this.setPrompt("提交账间转账成功");
		return SUCCESS;
	}
	
	@Action(value="removeTransfer",results={@Result(name="success",type="redirect",location="alterTransfer"),
			@Result(name="error",type="chain",location="alterTransfer")})
	public String removeTransfer() {
		if(ids == null) {
			this.addActionError("请选择要删除的账间转账信息");
			return ERROR;
		}
		this.getTransferAccountService().batchRemove(ids);
		return SUCCESS;
	}
	
	public TransferAccount getTransferAccount() {
		return transferAccount;
	}
	public void setTransferAccount(TransferAccount transferAccount) {
		this.transferAccount = transferAccount;
	}
	public TransferAccountService getTransferAccountService() {
		return transferAccountService;
	}
	public void setTransferAccountService(TransferAccountService transferAccountService) {
		this.transferAccountService = transferAccountService;
	}
	public PaginationSupport getPagination() {
		return pagination;
	}
	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}
	public Integer[] getIds() {
		return ids;
	}
	public void setIds(Integer[] ids) {
		this.ids = ids;
	}

	public void setBankAccountService(BankAccountService bankAccountService) {
		this.bankAccountService = bankAccountService;
	}

	public BankAccountService getBankAccountService() {
		return bankAccountService;
	}

	public void setBankAccounts(Collection<BankAccount> bankAccounts) {
		this.bankAccounts = bankAccounts;
	}

	public Collection<BankAccount> getBankAccounts() {
		return bankAccounts;
	}

	public void setUpload(File upload) {
		this.upload = upload;
	}

	public File getUpload() {
		return upload;
	}

	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}

	public String getUploadFileName() {
		return uploadFileName;
	}

	public String getBank() {
		return bank;
	}

	public void setBank(String bank) {
		this.bank = bank;
	}
	
}
